package com.hightemperator.service;

import com.hightemperator.dao.AssistMapper;
import com.hightemperator.dao.CircleMapper;
import com.hightemperator.dao.CommendMapper;
import com.hightemperator.po.*;
import com.sun.xml.internal.bind.v2.TODO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CircleService {

    @Autowired
    public AssistMapper assistMapper;
    @Autowired
    public CommendMapper commendMapper;
    @Autowired
    public CircleMapper circleMapper;
    @Autowired
    public HomePageService homePageService;


    /**
     * 点赞
     * 判断在点赞表中是否有 该用户在该动态中的点赞
     * 如果没有 添加点赞并点赞数+1
     * 如果有返回操作失败
     * 加入消息暂时未完成
     *
     * @param assist
     * @return
     */
    public int updateAssist(Assist assist) {

        int successCode = 0;
        Map<String, Object> param = new HashMap<>();
        param.put("circleId", assist.getCircleid());
        param.put("userId", assist.getUserid());

        Assist assistHistory = assistMapper.getAssistHistory(param);
        Circle circle = circleMapper.selectByPrimaryKey(assist.getCircleid());

        if (assistHistory == null) {
            successCode = assistMapper.insertSelective(assist);
            circle.setPraisecount((circle.getPraisecount() != null ? circle.getPraisecount() : 0) + 1);
        } else {
            successCode = assistMapper.deleteByPrimaryKey(assistHistory.getId());
            circle.setPraisecount((circle.getPraisecount() != null ? circle.getPraisecount() : 0) - 1);
        }

        if (successCode != 0) {

            CircleExample example1 = new CircleExample();
            CircleExample.Criteria criteria = example1.createCriteria();
            criteria.andIdEqualTo(circle.getId());
            successCode = circleMapper.updateByExample(circle, example1);
            successCode = circleMapper.updateByExample(circle, example1);
            successCode = circleMapper.updateByExample(circle, example1);
            if (successCode != 0) {
                //当点赞成功的时候 给朋友圈发布者发送一条消息
                successCode = homePageService.insertMessage(assist.getUserid(), assist.getCreateuserid()
                        , assist.getCircleid(), 0, "", "3");
            }
        }
        return successCode;
    }

    /**
     * 添加评论
     * 添加评论并评论数+1
     * 添加消息
     * 加入消息暂时未完成
     *
     * @param commend
     * @return
     */
    public int addCommend(Commend commend) {
        int successCode = commendMapper.insertSelective(commend);
        if (successCode != 0) {
            Circle circle = circleMapper.selectByPrimaryKey(commend.getCircleid());
            circle.setPraisecount((circle.getCommentcount() != null ? circle.getCommentcount() : 0) + 1);
            CircleExample example = new CircleExample();
            CircleExample.Criteria criteria = example.createCriteria();
            criteria.andIdEqualTo(circle.getId());
            successCode = circleMapper.updateByExample(circle, example);
            if (successCode != 0) {
                successCode = homePageService.insertMessage(commend.getCommenduserid(), commend.getCreatorcircleuserid()
                        , commend.getCircleid(), 0, "", "2");
            }
        }
        return successCode;
    }

    /**
     * 获取带有 头像的 点赞列表
     *
     * @param circleId
     * @return
     */
    public List<AssistAndUserMessage> getAssistList(Integer circleId) {
        Map<String, Object> param = new HashMap<>();
        param.put("id", circleId);
        param.put("begin", 0);
        param.put("end", 10);
        return assistMapper.getAssistMessage(param);
    }

    /**
     * 获取动态评论
     *
     * @param circleId
     * @return
     */
    public List<CommendList> getCommendList(Integer circleId, Integer uId) {
        return commendMapper.getCommendList(circleId, uId);
    }


}